package com.cnpinxi.controller.admin.distributionConfig;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.DistributionConfig;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.DistributionConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/distributionConfig")
@Controller
public class AdminDistributionConfigController extends BaseController {
    @Autowired
    DistributionConfigService distributionConfigService;
    /**
     * 查询优惠配置列表
     * @param request
     * @return
     */
    @RequestMapping("/distributionList")
    public ModelAndView distributionList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/distributionConfig/distributionList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<DistributionConfig> distributionConfigList = distributionConfigService.getDistributionConfigList(map);
        PageInfo<DistributionConfig> pageinfo = new PageInfo<>(distributionConfigList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }

    /**
     * 跳转到优惠配置编辑或添加页面
     * @param request
     * @return
     */
    @RequestMapping("/getDistributionConfig")
    public ModelAndView getDistributionConfig(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/distributionConfig/distribution");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtils.isNotBlank(tid)){
            DistributionConfig distributionConfig = distributionConfigService.getDistributionConfig(map);
            m.addObject("distributionConfig",distributionConfig);
        }
        return m;
    }

    /**
     * 添加或修改优惠配置
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifiyDistributionConfig")
    @ResponseBody
    public String modifiyDistributionConfig(HttpServletRequest request, DistributionConfig distributionConfig) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        Map<String, String> map = this.showRequestParameters(request);
        logger.info(distributionConfig.toString());
        try {
            if(map.get("flag")!=null){  //绑定产品实体
                DistributionConfig distributionConfig1 =  distributionConfigService.getDistributionConfig(map);
                distributionConfig1.setProductEntityId(Long.parseLong(map.get("productEntityId")));
                distributionConfigService.modifyDistributionConfig(distributionConfig1);
            }else{
                distributionConfigService.modifyDistributionConfig(distributionConfig);
            }
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delDistributionConfigById")
    @ResponseBody
    public String delDistributionConfigById(HttpServletRequest request, DistributionConfig distributionConfig) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        Map<String, String> map = this.showRequestParameters(request);
        logger.info(distributionConfig.toString());
        try {
            distributionConfig.setTid(Integer.parseInt(map.get("tid")));
            distributionConfigService.deleteDistributionConfig(distributionConfig);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
